# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>

# NOTE: This is a fork of LLVM project needed for emscripten. It provides
#   JSBackend and WebAssembly, these will be hopefully someday merged into
#   upstream LLVM.
# TODO: Remove unneeded binaries.
pkgname=emscripten-fastcomp
# Keep in sync with package emscripten!
pkgver=1.40.1
pkgrel=1
pkgdesc="The Clang + LLVM backend for Emscripten"
url="https://emscripten.org/"
# riscv64 is not supported upstream
arch="all !riscv64"
license="NCSA"
depends="python3"
makedepends="cmake zlib-dev"
source="emscripten-fastcomp-$pkgver.tar.gz::https://github.com/emscripten-core/emscripten-fastcomp/archive/$pkgver.tar.gz
	emscripten-fastcomp-clang-$pkgver.tar.gz::https://github.com/emscripten-core/emscripten-fastcomp-clang/archive/$pkgver.tar.gz
	llvm-fix-build-with-musl-libc.patch
	none-build-type.patch
	"
options="!check"  # some tests are broken, don't bother with it for now

_prefix="usr/lib/$pkgname"
ldpath="/$_prefix/lib"
# Avoid conflicts in provided so:* names with llvm package.
sonameprefix="$pkgname:"

prepare() {
	ln -sf ../../$pkgname-clang-$pkgver "$builddir"/tools/clang

	default_prepare

	update_config_guess
}

build() {
	case "$CARCH" in
		ppc64le) export CXXFLAGS="$CXXFLAGS -U__ALTIVEC__";;
	esac

	# XXX: Is it a good idea to omit X86 in LLVM_TARGETS_TO_BUILD?
	# https://github.com/kripken/emscripten-fastcomp/issues/174
	cmake -B build \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX="/$_prefix" \
		-DCMAKE_VERBOSE_MAKEFILE=NO \
		-DPYTHON_EXECUTABLE=/usr/bin/python3 \
		-DCLANG_BUILD_EXAMPLES=OFF \
		-DCLANG_INCLUDE_TESTS=OFF \
		-DLLVM_BUILD_DOCS=OFF \
		-DLLVM_BUILD_LLVM_DYLIB=ON \
		-DLLVM_BUILD_RUNTIME=OFF \
		-DLLVM_ENABLE_ZLIB=ON \
		-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly \
		-DLLVM_HOST_TRIPLE="$CHOST" \
		-DLLVM_INCLUDE_EXAMPLES=OFF \
		-DLLVM_INCLUDE_TESTS=OFF \
		-DLLVM_LINK_LLVM_DYLIB=ON \
		-DLLVM_TARGETS_TO_BUILD=JSBackend
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	cd "$pkgdir"/$_prefix

	# emcc -v expects this file to exist.
	cp "$builddir"/emscripten-version.txt .

	# Remove unneeded files and clutter.
	rm -r include libexec share lib/clang lib/cmake
	rm lib/*.a bin/*-test
}

sha512sums="569ed4b253db8b8305e93a0e3e3db18e64f7ad55da228b29a632290479c516aa8df6e20b8f695bbdcfdde14ba7be0c4fa27e54c5b1ded3a6cef0d59b3f1cb0d0  emscripten-fastcomp-1.40.1.tar.gz
f9abae060ebb391d1dd4f3b478cd2214318e333c820a5aca89fcf3bc30bd9903bd56d861c4c60d806ec6539fd340372f686e56d0750ddb06c54802a41b5b4ed7  emscripten-fastcomp-clang-1.40.1.tar.gz
49e9a3ddf9b1c881c70af4b5c6c99d087733453610f7b2733f91733e92b6097a2f427e496a1374f0f051b8429465de5f1f47d2e08239dca721b59333fc842925  llvm-fix-build-with-musl-libc.patch
b0d692ae595ea72cf6950b7d152c83a301c2f24178ec800a5e9c365961c78067512d0cdedb129fa0e528217a63d861daf6b9e2105d0309f65d3936a779a79144  none-build-type.patch"
